package com.sports.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sports.entity.*;
import com.sports.service.ItemService;
import com.sports.service.JudgeService;
import com.sports.service.impl.JudgeItemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: JudgeController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author WGH
 * @Create 2024/3/26 21:16
 * @Version 1.0
 */
@RestController
@RequestMapping("judge")
public class JudgeController {

    @Autowired
    JudgeService judgeService;

    @Autowired
    JudgeItemServiceImpl judgeItemService;

    @Autowired
    ItemService itemService;
    
    @PostMapping("addJudge")
    public Map  addJudge(String password1,@RequestBody Judge judge){
        Map map = new HashMap();
        if (!password1.equals(judge.getPassword())){
            map.put("message","not-same");
            return map;
        }
        judgeService.save(judge);
        map.put("message","success");
        return map;
    }

    @DeleteMapping("deleteJudge/{judgeId}")
    public Map deleteJudge(@PathVariable("judgeId") Integer judgeId){
        Map map = new HashMap();
        boolean removeById = judgeService.removeById(judgeId);
        if (!removeById) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        return map;
    }

    @PutMapping("updateJudge")
    public Map updateJudge(@RequestBody Judge judge){
        Map map = new HashMap();
        boolean updateById = judgeService.updateById(judge);
        if (!updateById) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        return map;
    }

    @GetMapping("getJudge/{judgeId}")
    public Map getJudge(@PathVariable("judgeId") Integer judgeId){
        Map map = new HashMap();
        Judge judge = judgeService.getById(judgeId);
        if (judge == null) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        map.put("judge",judge);
        return map;
    }

    @GetMapping("getAllJudge/{pageNum}/{pageSize}")
    public Map getAllJudge(
            @RequestParam(value = "name",required = false) String name,
            @PathVariable(value = "pageNum") int pageNum,
            @PathVariable(value = "pageSize") int pageSize){
        Map map = new HashMap();
        LambdaQueryWrapper<Judge> wrapper = new LambdaQueryWrapper<Judge>();
        if (!ObjectUtils.isEmpty(name)) {
            wrapper.like(Judge::getJudgeName, name);
        }
        Page<Judge> page = new Page<>(pageNum, pageSize);
        Page<Judge> page1 = judgeService.page(page,wrapper);
        List<Judge> list = page1.getRecords();
        long page1Size = page1.getSize();
        long current = page1.getCurrent();
        long total = page1.getTotal();
        map.put("message","success");
        map.put("list",list);
        map.put("pageSize",page1Size);
        map.put("pageNum",current);
        map.put("total",total);
        return map;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteJudges")
    public Map  deleteAthletes(@RequestBody int[] ids){
        boolean flags = judgeService.removeByIds(Arrays.asList(ids));
        Map map = new HashMap();
        if (!flags) {
            map.put("message","failed");
            return map;
        }
        map.put("message","success");
        return map;
    }

    @GetMapping("getJudgeItemList/{judgeId}")
    public Map getJudgeItemList(@PathVariable("judgeId") Integer judgeId){
        Map map = new HashMap();
        List<JudgeItemDTO> judgeItemList = judgeService.getJudgeItemList(judgeId);
        map.put("message","success");
        map.put("judgeItemList",judgeItemList);
        return map;
    }

    @DeleteMapping("deleteJudgeItem/{judgeId}/{itemId}")
    public Map deleteJudgeItem(@PathVariable("itemId") Integer itemId,@PathVariable("judgeId") Integer judgeId){
        Map map = new HashMap();
        boolean remove = judgeItemService.remove(new LambdaQueryWrapper<JudgeItem>()
                .eq(JudgeItem::getJudgeId, judgeId)
                .eq(JudgeItem::getItemId, itemId));
        if (!remove) {
            map.put("message","nothing");
            return map;
        }
        map.put("message","success");
        return map;
    }

    @PostMapping("addJudgeItem")
    public Map addJudgeItem(@RequestBody JudgeItemDTO judgeItemDTO){
        Map map = new HashMap();
        Integer itemId = null;
        try {
            itemId = itemService.getOne(new LambdaQueryWrapper<Item>()
                    .eq(Item::getItemName, judgeItemDTO.getItemName())).getItemId();
        } catch (Exception e) {
            map.put("message","failed1");
            e.printStackTrace();
            return map;
        }
        JudgeItem judgeItem = new JudgeItem(judgeItemDTO.getJudgeId(), itemId);
        try {
            boolean save = judgeItemService.save(judgeItem);
        } catch (Exception e){
            map.put("message","failed");
            e.printStackTrace();
            return map;
        }
        map.put("message","success");
        return map;
    }
}